/**
 * 
 */
package com.aqarat.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Building;
import com.aqarat.entity.Expens;
import com.core.managedBean.BaseBean;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class ExpensesBean extends BaseBean {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AqaratController aqaratController;
	private Expens expens;
	private List<Expens> expensesList;
	private Long biulgingId;
	private List<Building> buildingList;

	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		loadExpensesDetails();
		loadAllBuildings();
	}

	public void loadExpensesDetails() {
		Object expensObject = super.getFlashAttribute("expens");
		if (expensObject == null) {
			expens = new Expens();
		} else {
			expens = (Expens) expensObject;
		}
	}

	public void saveExpenses() {
		try {
			aqaratController.saveExpenses(expens);
			super.addInfoMessage("successfully Saved");
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}
	}

	public void loadExpenses() {
		try {
			if (biulgingId != null) {
				expensesList = aqaratController.getExpensesList(biulgingId);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void loadAllBuildings() {
		try {
			buildingList = aqaratController.getBuildingList(new Building());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public final Expens getExpens() {
		return expens;
	}

	public final void setExpens(Expens expens) {
		this.expens = expens;
	}

	public void setExpensesList(List<Expens> expensesList) {
		this.expensesList = expensesList;
	}

	public List<Expens> getExpensesList() {
		return expensesList;
	}

	public void setBiulgingId(Long biulgingId) {
		this.biulgingId = biulgingId;
	}

	public Long getBiulgingId() {
		return biulgingId;
	}

	public void setBuildingList(List<Building> buildingList) {
		this.buildingList = buildingList;
	}

	public List<Building> getBuildingList() {
		return buildingList;
	}
}
